package com.kuaiditu.assistant.api.xz_api.xz_encode.xz_http;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.kdy_api.kdy_encode.KdyMD5Util;
import com.kuaiditu.assistant.api.xz_api.xz_encode.MD5Encode;
import com.kuaiditu.assistant.api.xz_api.xz_encode.XingZheEncodeUtil;
import com.kuaiditu.assistant.api.xz_api.xz_encode.a;
import com.kuaiditu.assistant.api.zzt_api.zzt_encode.DataDigestUtil;
import com.kuaiditu.assistant.entity.Operator;
import com.kuaiditu.assistant.util.LogUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;

/**
 * Created by Eenie on 2017/8/11.
 * Email: eeniework@gmail.com
 * Des:
 */

public class FormBodyBuilderFactory {
    public static FormBody.Builder create(Integer cmd, Object data, Map<String, String> extMap, String pathCode, String uid) {
        XzBaseRequest request = new XzBaseRequest();
        FormBody.Builder builder = new FormBody.Builder();
        if (cmd != 0) {
            request.setCmd(cmd);
        }
        if (!TextUtils.isEmpty(uid)) {
            request.setUuid(uid);
        }
        request.setVersion("33");
        if (extMap != null) {
            request.setExtMap(extMap);
        }
        if (data != null) {
            if (data instanceof List) {
                request.setLst((List) data);
            } else {
                request.setObj(data);
            }
        }
        String k = "";
        String s = "";
        try {

            String src = JSON.toJSONStringWithDateFormat(request, "yyyy-MM-dd HH:mm:ss");

            LogUtil.e("xz_req = " + src);

            k = new String(a.a(XingZheEncodeUtil.encode(src, "9048").getBytes("utf-8")));
            s = URLEncoder.encode(MD5Encode.a(k), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        builder.add("k", k);
        builder.add("s", s);
        return builder;
    }


    public static Map<String, String> create(Object data) {
        Map<String, String> params = new HashMap<>();
        String content = JSON.toJSONString(data);
        params.put("content", content);
        params.put("token", KdyMD5Util.toMD5(content + "bac500a42230c8d7d1820f1f1fa9b578"));
        return params;
    }

    public static Map<String, String> createZTBody(String method, Object data, Operator operator) {
        String dataStr = JSON.toJSONString(data);
        Map<String, String> params = new HashMap<>();
        params.put("msg_type", method);
        params.put("data", dataStr);
        params.put("company_id", "APP_ANDROID");
        params.put("data_digest", DataDigestUtil.digest(dataStr, operator.getZt_token(), "UTF-8"));
        params.put("open_id", operator.getZt_openId());
        params.put("version", "4.1.0");
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        return params;
    }


    public static Map<String, String> create(Integer cmd, Object data, Map<String, String> extMap, String uid) {
        XzBaseRequest request = new XzBaseRequest();

        Map<String, String> build = new HashMap<>();


        if (cmd != 0) {
            request.setCmd(cmd);
        }
        if (!TextUtils.isEmpty(uid)) {
            request.setUuid(uid);
        }
        request.setVersion("33");
        if (extMap != null) {
            request.setExtMap(extMap);
        }
        if (data != null) {
            if (data instanceof List) {
                request.setLst((List) data);
            } else {
                request.setObj(data);
            }
        }
        String k = "";
        String s = "";
        try {
            String src = JSON.toJSONStringWithDateFormat(request, "yyyy-MM-dd HH:mm:ss");
            LogUtil.e("xz_req = " + src);
            k = new String(a.a(XingZheEncodeUtil.encode(src, "9048").getBytes("utf-8")));
            s = URLEncoder.encode(MD5Encode.a(k), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        build.put("k", k);
        build.put("s", s);


        return build;
    }


}
